window._CHESSBUTTON = {


    _SENCE:null,

    setSence:function(sence){

        _CHESSBUTTON._SENCE = sence;

    },

    initButton:function(){

        //战斗按钮
        _CHESSBUTTON._SENCE.fightSelectMask.on(cc.Node.EventType.TOUCH_START, function(){
            this.cancelCommond();
        }, _CHESSBUTTON._SENCE); 

        _CHESSBUTTON._SENCE.btnAttack.on(cc.Node.EventType.TOUCH_START, function(){
            this.currentCommond = _MCF._ATYPE_ATTACK;
            _MCFW.getCurrentPlayerAttackFanwei(true);

        }, _CHESSBUTTON._SENCE); 

        _CHESSBUTTON._SENCE.btnSkill.on(cc.Node.EventType.TOUCH_START, function(){
            var skillArr = _ROLEDATA.getSkillOfplayer(this.currentPlayer);
            skillArr.sort((a,b)=>{
                var skilltypea = _SKILL[a]?1:0;
                var skilltypeb = _SKILL[b]?1:0;
                if(skilltypea==skilltypeb){
                    return 0;
                }else{
                    if(skilltypea==1){
                        return -1;
                    }else{
                        return 1;
                    }
                }
            });
            _SKILLSELECTMANAGER.showSelect(this.node,skillArr,'skill');
        }, _CHESSBUTTON._SENCE); 

        _CHESSBUTTON._SENCE.btnWait.on(cc.Node.EventType.TOUCH_START, function(){
            this.waitAction();
        }, _CHESSBUTTON._SENCE); 

        _CHESSBUTTON._SENCE.btnItem.on(cc.Node.EventType.TOUCH_START, function(){

            if(_MTZ.isTzModel()){
                _TIPS.showMessage('挑战模式下无法使用道具');
                return ;
            }

            //_TIPS.showMessage('道具');
            //选择前先对道具排序
            window.gameData.fightItem.sort((a,b)=>{
                var typea = a.type;
                var typeb = b.type;
                var confa = _FIGHTITEM[typea];
                var confb = _FIGHTITEM[typeb];

                if(confa&&confb){
                    if(confa.skilltype==confb.skilltype){
                        return 0;
                    }else if(_FIGHTITEMHELPER.canUseInFight(typeb)){
                        return 1;
                    }else {
                        return -1;
                    }
                }
                return 0;
            });
            _SKILLSELECTMANAGER.showSelect(this.node,window.gameData.fightItem,'item');
        }, _CHESSBUTTON._SENCE); 


        //功能按钮
        _CHESSBUTTON._SENCE.btnAuto.on(cc.Node.EventType.TOUCH_END, function(){
            _CHESSBUTTON.switchAutoFight();
        }, _CHESSBUTTON._SENCE); 

        _CHESSBUTTON._SENCE.btnLock.on(cc.Node.EventType.TOUCH_END, function(){
            _CHESSBUTTON.switchLockLevel();
        }, _CHESSBUTTON._SENCE); 

        _CHESSBUTTON._SENCE.btnSpeed.on(cc.Node.EventType.TOUCH_END, function(){
            _CHESSBUTTON.switchFightSpeed();
        }, _CHESSBUTTON._SENCE); 

        _CHESSBUTTON._SENCE.btnSwitch.on(cc.Node.EventType.TOUCH_END, function(){
            _CHESSBUTTON.switchMenu();
        }, _CHESSBUTTON._SENCE); 

        _CHESSBUTTON._SENCE.btnCheck.on(cc.Node.EventType.TOUCH_END, function(){
            this.isCheck = true;
            this.menuLabel.active = false;
        }, _CHESSBUTTON._SENCE); 

        _CHESSBUTTON._SENCE.btnRun.on(cc.Node.EventType.TOUCH_END, function(){
            _CHOICEMANAGER.showChoice(this.node,'quitChessFight',null);
        }, _CHESSBUTTON._SENCE); 
        

        _CHESSBUTTON._SENCE.btnAuto.getChildByName('btn_img_zidong').active = !_CHESSBUTTON._SENCE.autofight;
        _CHESSBUTTON._SENCE.btnAuto.getChildByName('btn_img_tinzi').active = _CHESSBUTTON._SENCE.autofight;
        _CHESSBUTTON._SENCE.btnLock.getChildByName('lock').active = window.gameData.lockChapterLevel?true:false;
        _CHESSBUTTON._SENCE.btnLock.getChildByName('unlock').active = window.gameData.lockChapterLevel?false:true;
    },


    //切换控制按钮位置
    switchMenu:function(){
        var menuposition = _CHESSBUTTON._SENCE.menuLabel.getPosition();
        _CHESSBUTTON._SENCE.menuLabel.setPosition(-menuposition.x,menuposition.y);

    },

    //切换速度
    switchFightSpeed:function(){

        if(window.gameData.fightSpeed<3){
            window.gameData.fightSpeed++;
        }else{
            window.gameData.fightSpeed=1;
        }

        _CHESSBUTTON.setFightSpeed();        
    },

    //设置速度
    setFightSpeed:function(){

        for(var i=1;i<=3;i++){
            _CHESSBUTTON._SENCE.btnSpeed.getChildByName('btn_img_x'+i).active = (i==window.gameData.fightSpeed);
        }

        cc.director.getScheduler().setTimeScale(window.gameData.fightSpeed);

    },

    //切换自动战斗
    switchAutoFight:function(){
        var chessFight = _CHESSBUTTON._SENCE;
        window.gameData.autofight = !window.gameData.autofight;
        chessFight.autofight = !chessFight.autofight;
        if(chessFight.autofight&&chessFight.canClick){
            if(chessFight.onlyMove){
                chessFight.moveAgainToTarget()
            }else{
                _MCA.aiAction();
            }
        }
        window.fightData.autofight = chessFight.autofight;
        chessFight.btnAuto.getChildByName('btn_img_zidong').active = !chessFight.autofight;
        chessFight.btnAuto.getChildByName('btn_img_tinzi').active = chessFight.autofight;
    },

    //切换锁定层数
    switchLockLevel:function(){
        var chessFight = _CHESSBUTTON._SENCE;
        if(window.gameData.lockChapterLevel){
            window.gameData.lockChapterLevel = false;
        }else{
            window.gameData.lockChapterLevel = true;
        }
        chessFight.btnLock.getChildByName('lock').active = window.gameData.lockChapterLevel;
        chessFight.btnLock.getChildByName('unlock').active = !window.gameData.lockChapterLevel;
    },


}